package com.ruoyi.web.controller.wechatApplet;


import com.ruoyi.background.domain.AppCompanyRules;
import com.ruoyi.background.service.IAppCompanyRulesService;
import com.ruoyi.background.utils.REDIS_KEY;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 规章制度
 */
@RestController
@RequestMapping("/wechat/companyRules")
public class CompanyRulesController extends BaseController {

    @Autowired
    private IAppCompanyRulesService appCompanyRulesService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取置顶
     *
     * @param appCompanyRules 查询条件
     * @return list
     */
    @GetMapping("getTopList")
    public TableDataInfo listTop(AppCompanyRules appCompanyRules) {

        String key = REDIS_KEY.RULES_TOP_LIST_KEY;
        List<AppCompanyRules> companyRulesList = redisCache.getCacheList(key);
        if (ObjectUtils.isEmpty(companyRulesList)) {
            appCompanyRules.setStatus(0);
            appCompanyRules.setSetTop(1);
            companyRulesList = appCompanyRulesService.selectAppCompanyRulesList(appCompanyRules);
            redisCache.deleteByKey(key);
            redisCache.setCacheList(key, companyRulesList);
            redisCache.expire(key, 48 * 60, TimeUnit.MINUTES);
        }
        return getDataTable(companyRulesList);
    }

    /**
     * 获取规章制度列表
     *
     * @param appCompanyRules 查询条件
     * @return list
     */
    @GetMapping("getList")
    public TableDataInfo list(AppCompanyRules appCompanyRules) {
        appCompanyRules.setStatus(0);
        startPage();
        List<AppCompanyRules> list = appCompanyRulesService.selectAppCompanyRulesList(appCompanyRules);
        return getDataTable(list);
    }

    /**
     * 通过Id获取详情
     *
     * @param id id
     * @return 结果
     */
    @GetMapping("/get/{id}")
    public AjaxResult getRulesById(@PathVariable Long id) {
        AppCompanyRules rules = appCompanyRulesService.selectAppCompanyRulesById(id);
        if (1 == rules.getDelFlag()) return AjaxResult.error("规章制度已删除！");
        if (1 == rules.getStatus()) return AjaxResult.error("规章制度已下架");
        return AjaxResult.success(rules);
    }


}
